from flask import Flask
from .config import *
from .exts import init_exts
from .urls import *
from .intercept import init_interceptor


def set_db_config(app):
    config = create_config()
    db_uri = f"mysql+pymysql://{config.db_config['USERNAME']}:{config.db_config['PASSWORD']}@{config.db_config['HOST']}:{config.db_config['PORT']}/{config.db_config['DB_NAME']}"
    # 配置连接数据库的路径 DB_URI
    app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
    # 禁止对象追踪修改
    app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False



def set_redis_config(app):
    config = create_config()
    redis_url = f"redis://{config.redis_config['HOST']}:{config.redis_config['PORT']}/{config.redis_config['DB']}"
    app.config['REDIS_URL'] = redis_url


def create_app():
    app = Flask(__name__)
    set_db_config(app)
    set_redis_config(app)
    init_exts(app)
    init_interceptor(app)
    return app
